
//
//  ReadingViewController.m
//  XWZ_News_Work
//
//  Created by penngshi on 16/1/12.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ReadingViewController.h"
#import "readModel.h"
#import "tempOneTableViewCell.h"
#import "tempTwoTableViewCell.h"
#import "tempThreeTableViewCell.h"
#import "ReadingWebViewController.h"

#define URL @"http://c.3g.163.com/recommend/getSubDocPic?passport=&devId=866479021081277&size=20&version=5.3.6&from=yuedu&net=wifi"
#define URL_UP(a, b) [NSString stringWithFormat:@"http://c.3g.163.com/nc/video/list/%@/n/%ld-10.html", a, b]


@interface ReadingViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableV;
@property (nonatomic, strong) NSMutableArray *myArr;
@property (nonatomic, assign) NSInteger page;

@end

@implementation ReadingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self navigationBarAdjust];
    [self createTableView];
}

- (void)navigationBarAdjust
{
    self.navigationItem.title =  @"阅读推荐";
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    self.navigationController.navigationBar.translucent = NO;
    self.navigationController.navigationBar.barTintColor = [UIColor redColor];
    self.navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:17],NSFontAttributeName, [UIColor whiteColor], NSForegroundColorAttributeName, nil];
}

- (void)createTableView
{
    self.tableV = [[UITableView alloc] initWithFrame:CGRectZero style:(UITableViewStylePlain)];
    self.tableV.delegate = self;
    self.tableV.dataSource = self;
    self.tableV.separatorStyle = NO;
    [self.view addSubview:self.tableV];
    [self.tableV mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.edges.equalTo(self.view);
        
    }];
    
#pragma 下拉
    self.tableV.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
       
        [self createData];
        [self.tableV reloadData];
        
    }];
    [self.tableV.mj_header beginRefreshing];
    
#pragma 上拉
    if ((self.tableV.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(createUPData)])) {

    }
}

- (void)createUPData
{
    [self createMoreDataWithUrl:@"http://c.3g.163.com/recommend/getSubDocPic?passport=&devId=866479021081277&size=20&version=5.3.6&from=yuedu&net=wifi"];
}

- (void)createMoreDataWithUrl:(NSString *)url
{
    [xwzAFNetTool getNetWithURL:URL body:nil headFile:nil responStyle:(XWZ_JSON) success:^(NSURLSessionDataTask *task, id responseObject) {
        
        NSDictionary *dic = responseObject;
        NSArray *arr = dic[@"推荐"];
        for (NSDictionary *dic in arr) {
            readModel *readM = [[readModel alloc] init];
            [readM setValuesForKeysWithDictionary:dic];
            [self.myArr addObject:readM];
        }
        
        [self.tableV.mj_footer endRefreshing];
        [self.tableV reloadData];
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.myArr.count;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    readModel *model = [self.myArr objectAtIndex:indexPath.row];
    ReadingWebViewController *webViewVC = [[ReadingWebViewController alloc] init];
    webViewVC.code = model.docid;
    [self.navigationController pushViewController:webViewVC animated:YES];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"hideView" object:nil];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    readModel *model = [self.myArr objectAtIndex:indexPath.row];
    if (model.imgnewextra) {
        static NSString *cellStr = @"cellOne";
        NSLog(@"%@", model.img);
        tempOneTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellStr];
        if (cell == nil) {
            cell = [[tempOneTableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:cellStr];
        }
        [cell.imageLeft sd_setImageWithURL:[NSURL URLWithString:model.img] placeholderImage:[UIImage imageNamed:@"lol-tupian"]];
        [cell.imageRightTop sd_setImageWithURL:[NSURL URLWithString:[[model.imgnewextra firstObject] objectForKey:@"imgsrc"]] placeholderImage:[UIImage imageNamed:@"lol-tupian"]];
        [cell.imageRightBelow sd_setImageWithURL:[NSURL URLWithString:[[model.imgnewextra lastObject] objectForKey:@"imgsrc"]] placeholderImage:nil];
        cell.selectionStyle = NO;
        cell.titleLab.text = model.title;
        cell.sourceLab.text = model.source;
        return cell;
    } 
    static NSString *cellStr = @"cellThree";
    tempThreeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellStr];
    if (cell == nil) {
        cell = [[tempThreeTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellStr];
    }
    [cell.imageV sd_setImageWithURL:[NSURL URLWithString:model.img] placeholderImage:nil];
    cell.titleLab.text = model.title;
    cell.sourceLab.text = model.source;
    cell.selectionStyle = NO;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    readModel *model = [self.myArr objectAtIndex:indexPath.row];
    if (model.imgnewextra) {
        return 320;
    }
    return 150;
}

- (void)createData
{
    [xwzAFNetTool getNetWithURL:URL body:nil headFile:nil responStyle:(XWZ_JSON) success:^(NSURLSessionDataTask *task, id responseObject) {
       
        NSMutableArray *recomdArr = [responseObject objectForKey:@"推荐"];
        self.myArr = [readModel baseModeWithArr:recomdArr];
        if (self.myArr) {
            [self.tableV reloadData];
        }
        [self.tableV.mj_header endRefreshing];
        
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
